package com.dex4d.ui.main.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.dex4d.R;
import com.dex4d.glide.GlideUtil;
import com.dex4d.ui.main.model.TokenGame;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者    LiQingyi
 * 时间    2018/11/15 10:20
 * 文件    dex4d-android
 * 描述
 */
public class TokenGameAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Activity context;
    private LayoutInflater inflater;
    private List<TokenGame> gameList;

    public TokenGameAdapter(Activity context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.gameList = new ArrayList<>();
    }

    public void addData(List<TokenGame> list) {
        if (list == null || list.isEmpty())
            return;
        this.gameList.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.acount_item_game, viewGroup, false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        VH vh = (VH) viewHolder;
        TokenGame game = gameList.get(i);
        GlideUtil.displayGameImage(vh.gameIcon, game.logo);
    }

    @Override
    public int getItemCount() {
        return gameList == null ? 0 : gameList.size();
    }

    static class VH extends RecyclerView.ViewHolder {

        @BindView(R.id.img_game_icon)
        ImageView gameIcon;

        public VH(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }
}
